; prolog.asm
; Basic start code & time initialisation

	.include "global.inc"
	.include "vicrom.inc"
	.include "colors.inc"
	.include "eightplay.inc"

	.macpack cbm

	.import main
	.import check_key
	.import adaptr
	
	.import pl_IInit
	.import pl_UUnInit
	.import pl_PPlay

	COLOR_CYCLE_SCANS = 2
	SCRLNLEN = COLS
	SCRLNST = VIDEO_BASE + (ROWS-2)*COLS 

	.segment "BASIC"

; Startup Basic program "2010 SYS main"
			.word	basprg
basprg:		.word	@end
			.word	2012
			.byte	$9E
			.byte	<(intro / 1000 .mod 10) + $30
			.byte	<(intro / 100 .mod 10) + $30
			.byte	<(intro / 10 .mod 10) + $30
			.byte	<(intro / 1 .mod 10) + $30
			.byte	0
@end:		.word	0

saveF:		.res 1
scancnt:	.res 1

	stepsize = 1
	stepcnt = $50 / stepsize
	
	palline = $24
	ntscline = $17  

intro:		sei
			lda #$7F				; disable all interrupts (NMI)
			sta VIA1_BASE+14
			
			lda VIC_BASE
			sta tmpptr1
			sta tmpptr2

			; setup for PAL/NTSC
			lda MACHINE			; machine detection by r.hurst
			cmp #M_NTSC
			beq @ntsc
			ldy #palline
			.byte $2C ; BIT ABS
@ntsc:		ldy #ntscline
			ldx #lnconvcnt-1
@ntvidlp:	clc
			tya
			adc lintab, x
			sta lintab, x
			dex
			bpl @ntvidlp
			
			ldx #0
@clrvid:	lda title, x	
			sta VIDEO_BASE, x
			lda title+$0100, x
			sta VIDEO_BASE+$0100, x
			inx
			bne @clrvid
			lda #BLACK		
			ldx #0
@clrclr:	sta COLOR_BASE, x
			sta COLOR_BASE+$0100, x			
			inx
			bne @clrclr
			lda VIC_BASE+15
			sta saveF
			lda #COLOR_CYCLE_SCANS
			sta scancnt
			jsr pl_IInit
			
@waitkey1:	ldy #0
@linlp:		lda lintab, y
			tax
			lda clrtab, y
@l1sync:	cpx VIC_BASE+4
			bne @l1sync
			sta VIC_BASE+15
			iny
			cpy #stepcnt
			bcc @linlp
			
			lda #16*ORANGE
			sta VIC_BASE+15
			
			lda tmpptr1
			eor #1
			sta VIC_BASE
			sta tmpptr1
			and #1
			bne @odd
			inc @scrtxlp+1
@odd:		
			ldx scrln1
			lda #16*LIGHTORANGE
@l2sync:	cpx VIC_BASE+4
			bne @l2sync
			sta VIC_BASE+15
			ldx scrln2
			lda #16*YELLOW
@l3sync:	cpx VIC_BASE+4
			bne @l3sync
			sta VIC_BASE+15
			ldx scrln3
			lda #16*WHITE
@l4sync:	cpx VIC_BASE+4
			bne @l4sync
			sta VIC_BASE+15
			
			jsr pl_PPlay

			dec scancnt
			bne @skip
			lda #COLOR_CYCLE_SCANS
			sta scancnt
			
			ldx #stepcnt-1
@clrlp:		clc
			lda clrtab, x
			lsr
			lsr
			lsr
			lsr
			tay
			lda nxttab, y
			sta clrtab, x		
			dex
			bpl @clrlp
			
@skip:		ldy #SCRLNLEN-1
@scrtxlp:	lda scrltxt, y
			sta SCRLNST, y
			dey
			bpl @scrtxlp

			lda tmpptr2
			sta VIC_BASE

			jsr check_key
			bne @done1
			jmp @waitkey1
@done1:
			; interface selection
			lda #32
			ldx #0
			stx VIC_BASE+15
@clrlp2:	sta VIDEO_BASE, x
			sta VIDEO_BASE+$0100, x
			inx
			bne @clrlp2

			lda choice1ps
			sta tmpptr1
			lda choice1ps+1
			sta tmpptr1+1
			ldy #0
@tx1loop:	lda choice1tx, y
			sta (tmpptr1), y
			iny
			cpy #choice1ln
			bne @tx1loop
						
			lda choice2ps
			sta tmpptr1
			lda choice2ps+1
			sta tmpptr1+1
			ldy #0
@tx2loop:	lda choice2tx, y
			sta (tmpptr1), y
			iny
			cpy #choice2ln
			bne @tx2loop

			lda #0
			sta tmpptr2

@waitkey2:	ldy tmpptr2
			lda choice1cr, y
			ldx chcln1
@c1sync:	cpx VIC_BASE+4
			bne @c1sync
			sta VIC_BASE+15

			lda choice2cr, y
			ldx chcln2
@c2sync:	cpx VIC_BASE+4
			bne @c2sync
			sta VIC_BASE+15

			jsr pl_PPlay

			jsr jread0
			and #JOY_FIRE
			beq @done2
			lda #JOY_UP
			bit joypos
			beq @doup
			lda #JOY_DOWN
			bit joypos
			bne @waitkey2
@dodown:	lda #1
			sta tmpptr2
			bne @waitkey2
@doup:		lda #0
			sta tmpptr2			
			beq @waitkey2
@done2:
			jsr pl_UUnInit
			
			ldy tmpptr2
			lda joyrdlo, y
			sta adaptr
			lda joyrdhi, y
			sta adaptr+1
			
@unclick:	jsr jread0
			and #$10
			beq @unclick
			
			lda saveF
			sta VIC_BASE+15
			
			lda tmpptr2
			jmp main

joyrdlo:	.byte <jread8, <jread4
joyrdhi:	.byte >jread8, >jread4

title:		.incbin "title.bin"

lintab:		.repeat stepcnt, I
				.byte I*stepsize
			.endrep
scrln1:		.byte $57
scrln2:		.byte $58
scrln3:		.byte $59
chcln1:		.byte $0
chcln2:		.byte $30

	lnconvcnt = * - lintab
 
clrtab:		.repeat (stepcnt+13)/14, I
				.byte 16*RED
				.byte 16*LIGHTRED
				.byte 16*LIGHTORANGE
				.byte 16*ORANGE
				.byte 16*YELLOW
				.byte 16*LIGHTYELLOW
				.byte 16*LIGHTGREEN
				.byte 16*GREEN
				.byte 16*BLUE
				.byte 16*LIGHTBLUE
				.byte 16*CYAN
				.byte 16*LIGHTCYAN
				.byte 16*PURPLE
				.byte 16*LIGHTPURPLE
			.endrep

nxttab:		.byte 16*BLACK			; <- BLACK
			.byte 16*WHITE			; <- WHITE
			.byte 16*LIGHTRED		; <- RED
			.byte 16*LIGHTCYAN		; <- CYAN
			.byte 16*LIGHTPURPLE	; <- PURPLE
			.byte 16*BLUE			; <- GREEN
			.byte 16*LIGHTBLUE		; <- BLUE
			.byte 16*LIGHTYELLOW	; <- YELLOW
			.byte 16*YELLOW			; <- ORANGE
			.byte 16*ORANGE			; <- LIGHTORANGE
			.byte 16*LIGHTORANGE	; <- LIGHTRED
			.byte 16*PURPLE			; <- LIGHTCYAN
			.byte 16*RED			; <- LIGHTPURPLE
			.byte 16*GREEN			; <- LIGHTGREEN
			.byte 16*CYAN			; <- LIGHTBLUE
			.byte 16*LIGHTGREEN		; <- LIGHTYELLOW

	.segment "TEXT" 

.macro linespace num
	.repeat num
		scrcode " "
	.endrep
.endmacro

scrltxt:	linespace SCRLNLEN
			scrcode "a multiplayer game for the vic-20  presented first at"
			linespace SCRLNLEN/2
			scrcode "*** commodore treffen graz $0b ***"
			linespace SCRLNLEN/2
			scrcode "intro music: slowride by daniel kahlin/ t l r"  
			linespace SCRLNLEN/2
			scrcode "everything else: k a n a n g a"
			linespace (256 + SCRLNLEN - (* - scrltxt))

choice1ps:	.word 7*22 + VIDEO_BASE
choice1tx:	scrcode "  8 player interface  "
			scrcode "  (up to 9 players)"
choice1ln = * - choice1tx
choice1cr:	.byte 16*WHITE, 16*YELLOW

choice2ps:	.word 15*22 + VIDEO_BASE
choice2tx:	scrcode " protovision 4 player "
			scrcode "  (up to 3 players)"
choice2ln = * - choice2tx
choice2cr:	.byte 16*YELLOW, 16*WHITE

